package com.wedding.app.controller;

import com.wedding.app.entity.OperationLog;
import com.wedding.app.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/log")
public class OperationLogController {

    @Autowired
    private OperationLogService operationLogService;

    // 获取所有操作日志
    @GetMapping("/list")
    public Map<String, Object> getLogList() {
        Map<String, Object> result = new HashMap<>();
        List<OperationLog> logList = operationLogService.selectAll();
        result.put("code", 200);
        result.put("message", "获取成功");
        result.put("data", logList);
        return result;
    }

    // 根据用户ID获取操作日志
    @GetMapping("/listByUserId/{userId}")
    public Map<String, Object> getLogListByUserId(@PathVariable Long userId) {
        Map<String, Object> result = new HashMap<>();
        List<OperationLog> logList = operationLogService.selectByUserId(userId);
        result.put("code", 200);
        result.put("message", "获取成功");
        result.put("data", logList);
        return result;
    }

    // 根据ID获取操作日志
    @GetMapping("/info/{id}")
    public Map<String, Object> getLogInfo(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        OperationLog log = operationLogService.selectById(id);
        if (log != null) {
            result.put("code", 200);
            result.put("message", "获取成功");
            result.put("data", log);
        } else {
            result.put("code", 400);
            result.put("message", "日志不存在");
        }
        return result;
    }

    // 删除操作日志
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> deleteLog(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        int count = operationLogService.deleteById(id);
        if (count > 0) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 400);
            result.put("message", "删除失败");
        }
        return result;
    }

    // 批量删除操作日志
    @DeleteMapping("/deleteBatch")
    public Map<String, Object> deleteLogBatch(@RequestBody Long[] ids) {
        Map<String, Object> result = new HashMap<>();
        int count = operationLogService.deleteByIds(ids);
        if (count > 0) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 400);
            result.put("message", "删除失败");
        }
        return result;
    }

    // 添加操作日志(通常由系统自动调用)
    @PostMapping("/add")
    public Map<String, Object> addLog(@RequestBody OperationLog log, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        // 获取IP地址
        String ip = request.getRemoteAddr();
        // 获取用户代理
        String userAgent = request.getHeader("User-Agent");
        log.setIp(ip);
        log.setUserAgent(userAgent);
        int count = operationLogService.insert(log);
        if (count > 0) {
            result.put("code", 200);
            result.put("message", "添加成功");
        } else {
            result.put("code", 400);
            result.put("message", "添加失败");
        }
        return result;
    }
}